package com.hs.srm.admin.modules.bills.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.bills.dao.ScmBillsDao;
import com.hs.srm.admin.modules.bills.entity.ScmBillsEntity;
import com.hs.srm.admin.modules.bills.service.ScmBillsService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import sunnykid.lang.SunnykidNumber;

/**
 * 对账单Service控制层实现类
 * <p>標題：</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:46:29
 * @since V1.0.0
 */
@Service("scmBillsService")
public class ScmBillsServiceImpl extends ServiceImpl<ScmBillsDao, ScmBillsEntity> implements ScmBillsService {

	@Autowired
	private ScmBillsDao scmBillsDao;

	@Override
	public PageUtils selectDetailPage(Map<String, Object> params) {
		// String username = (String)params.get("username");
		Page<ScmBillsEntity> page = this.selectDetailPage(new QueryParam<ScmBillsEntity>(params).getPage(),
				new EntityWrapper<ScmBillsEntity>(), params);
		return new PageUtils(page);
	}

	@Override
	public List<ScmBillsEntity> selectListByBillNo(String billNo) {
		EntityWrapper<ScmBillsEntity> queryWrapper = new EntityWrapper<>();
		queryWrapper.eq("bill_no", billNo);
		List<ScmBillsEntity> list = this.selectList(queryWrapper);
		return list;
	}

	private Page<ScmBillsEntity> selectDetailPage(Page<ScmBillsEntity> page, Wrapper<ScmBillsEntity> wrapper,
			Map<String, Object> params) {
		page.setRecords(this.baseMapper.selectDetailPage(page, wrapper, params));
		return page;
	}
	
	//数字100
    private static final BigDecimal HUNDRED = new BigDecimal(0x64);
	
    /**
     * 计算税额
     *
     * @param billMonoey  对账单金额
     * @param taxCodeText 税代码文字 (J0-0% 进项税，中国)
     */
    @Override
    public BigDecimal calTaxMoney(BigDecimal billMonoey, String taxCodeText) {
        String percentStr = null;
        try {
            percentStr = taxCodeText.substring(taxCodeText.indexOf("-") + "-".length(), taxCodeText.indexOf("%"));
        } catch(Throwable ex) {
        	throw new RuntimeException("税代码表述不正确", ex);
        }
        double value=billMonoey.doubleValue() / ((1 + (Double.valueOf(percentStr) / 100)));
        return billMonoey.subtract(new BigDecimal(new SunnykidNumber(String.format("%.2f",value)).parseSizedNumber(2)));
    }

	//更改stock状态
	@Override
	public boolean updateStockStatus(String billNo, int status) {
		return this.baseMapper.updateStockStatus(billNo,status);
	}

}
